package me.zhengjie.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.domain.vo.MesTemplateConfig;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.service.GenConfigService;
import me.zhengjie.service.GenCustomService;
import me.zhengjie.service.GeneratorService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description: 自定义代码生成管理
 * @author: mfc
 * @date: 2024/12/13 9:24
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/genCustom")
@Api(tags = "系统：自定义代码生成管理")
public class GenCustomController {

    private final GenCustomService genCustomService;
    private final GenConfigService genConfigService;


    @Value("${generator.enabled}")
    private Boolean generatorEnabled;

    @Value("${generator.templateFilePath}")
    private String templateFilePath;

    @ApiOperation("生成代码")
    @PostMapping(value = "/{templateNameEn}/{type}")
    public ResponseEntity<Object> generatorCode(@RequestBody List<MesTemplateConfig> templateConfigs, @PathVariable String templateNameEn, @PathVariable Integer type, HttpServletRequest request, HttpServletResponse response){
        if(!generatorEnabled && type == 0){
            throw new BadRequestException("此环境不允许生成代码，请选择预览或者下载查看！");
        }
        switch (type){
            // 生成代码
            case 0: genCustomService.generator(templateFilePath,templateNameEn,templateConfigs);
                break;
            // 预览
//            case 1: return genCustomService.preview(genConfigService.find(tableName), genCustomService.getColumns(tableName));
//            // 打包
            case 2: genCustomService.download(templateFilePath,templateNameEn,templateConfigs, request, response);
                break;
            default: throw new BadRequestException("没有这个选项");
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
